package com.shop.controller.admin;

import com.shop.dto.GoodsDTO;
import com.shop.dto.GoodsPageQueryDTO;
import com.shop.entity.Goods;
import com.shop.result.PageResult;
import com.shop.result.Result;
import com.shop.service.CategoryService;
import com.shop.service.GoodsService;
import com.shop.vo.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 商品管理
 */
@RestController
@Api(tags = "商品管理")
@Slf4j
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    private GoodsService GoodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    @ApiOperation("新增商品")
    public Result save(@RequestBody GoodsDTO GoodsDTO) {
        log.info("新增商品：{}",GoodsDTO);
        String key = ("goods_" + GoodsDTO.getCategoryId());
        cleanCache(key);
        GoodsService.saveWithFlavor(GoodsDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("商品分页查询")
    public Result<PageResult> page(GoodsPageQueryDTO GoodsPageQueryDTO) {
        log.info("商品分页查询：{}",GoodsPageQueryDTO);
        PageResult pageResult = GoodsService.pageQuery(GoodsPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 商品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("商品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("商品批量删除{}",ids);
        cleanCache("goods_*");
        GoodsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     *根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商品")
    public Result<GoodsVO> getById(@PathVariable Long id) {
        log.info("根据id查询商品：{}",id);
        GoodsVO GoodsVO = GoodsService.getByidWithFlavor(id);
        return Result.success(GoodsVO);
    }

    /**
     * 修改商品
     * @param GoodsDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品")
    public Result update(@RequestBody GoodsDTO GoodsDTO) {
        log.info("修改商品：{}",GoodsDTO);
        cleanCache("goods_*");
        GoodsService.updateWithFlavor(GoodsDTO);
        return Result.success();
    }

    /**
     * 起售停售商品
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售停售商品")
    public Result startOrStop(@PathVariable("status") Integer status,Long id) {
        log.info("起售停售商品:{},{}",status,id);
        cleanCache("goods_*");
        GoodsService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询商品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询商品")
    @GetMapping("/list")
    public Result<List<Goods>> list(Long categoryId) {
        List<Goods> list = GoodsService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 删除缓存
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}